var mySchedule = function (){

    var year,mon,day;
    var dateSum;
    var orderArr = [], orderSameArr = [];
    var defaults = {
        monthOrDay:'month',
        myDate:new Date,
        station:[],
        data:[],

        container:'',
        prevYearMonth:'',
        nextYearMonth:'',
        ClickNode :null,
        prevYearMonthClick:null,
        nextYearMonthClick:null,
        dayClick:null
    }
    this.Inite = function (options)
    {
        defaults = $.extend(defaults, options);
        var myDate = defaults.myDate;
        year = myDate.getFullYear(); //获取当前年
        mon = '' + (myDate.getMonth() + 1); //获取当前月
        day = '' + myDate.getDate(); //获取当前日
        if (mon.length < 2) mon = '0' + mon;
        if (day.length < 2) day = '0' + day;
        $(defaults.container).html(mon + "月" + ' ' + year);
        /*点击获取上一个月*/
        $(defaults.prevYearMonth).off('click').click(function () {
            if(defaults.monthOrDay == "month")
            {
                $(".stateChart").html('');
                getPreMonth($(defaults.container).html());
                getDateList($(defaults.container).html());
                tableLi(); // 画出对应表格
                statePosition();
            }
            else
            {
                $(".stateChart").html('');
                orderArr = [];
                orderSameArr = [];
                var currentDay = $(defaults.container).html().split(' ');// 当前显示的年月日
                var y = currentDay[0].replace("年", "");
                var m = currentDay[1].replace("月", "");
                var d = currentDay[2].replace("日", "");
                var ymd = y + "-" + m + "-" + d;
                var preDay = getNextDate(ymd, -1);
                statePosition_dayDetail();
            }

        })

        /*点击获取下一个月*/
        $(defaults.nextYearMonth).off('click').click(function () {
            if(defaults.monthOrDay == "month")
            {
                $(".stateChart").html('');
                getNextMonth($(defaults.container).html());
                getDateList($(defaults.container).html());
                tableLi(); // 画出对应表格
                statePosition();
            }
            else
            {
                $(".stateChart").html('');
                orderArr = [];
                orderSameArr = [];
                var currentDay = $(".currentYearMonth").html().split(' ');// 当前显示的年月日
                var y = currentDay[0].replace("年", "");
                var m = currentDay[1].replace("月", "");
                var d = currentDay[2].replace("日", "");
                var ymd = y + "-" + m + "-" + d;
                var nextDay = getNextDate(ymd, 1);
                statePosition_dayDetail();
            }

        })
        initeLeft();
    }

    this.refresh = function ()
    {
         $(".stateChart").html('');
          dateLiWidth();
          tableLi();
          statePosition();
    }

    var ChangeMonthOrDay = function (time)
    {
        $("#date").html('');
        $(".stateChart").html('')
        defaults.monthOrDay = 'day';
        $(".returnMonth").removeAttr("style", "");
        $(".content-right").addClass("dayDetail");
        $(defaults.container).html(time.year + "年 " + time.month + "月 " + time.day + "日");
        $(".date").append('<li><div class="dayStart">0:00</div><div class="dayEnd">24:00</div></li>');
        tableLi();
        statePosition_dayDetail();
    }

    var  initeLeft = function ()
    {
        $('.content-left-con').empty();
        var html = '';
        $.each(defaults.station, function (i, n) {
            html += '<li>' + n + '</li>';
        });
        $('.content-left-con').html(html);
        getDateList($(defaults.container).html());//遍历出当前月所有日期 并 给 date li 设置宽度
        statePosition();//定位状态条
        tableLi(); // 画出对应表格
    }

    var getPreMonth = function (date)
    {
        var arr = date.split('月 ');
        var year = arr[1]; //获取当前日期的年份
        var month = arr[0]; //获取当前日期的月份
        var year2 = year;
        var month2 = parseInt(month) - 1;
        if (month2 == 0) {
            year2 = parseInt(year2) - 1;
            month2 = 12;
        }
        month2 = '' + month2;
        if (month2.length < 2) month2 = '0' + month2;
        $(defaults.container).html(month2 + "月" + ' ' + year2);

    }

    var getNextMonth = function (date)
    {
        var arr = date.split('月 ');
        var year = arr[1]; //获取当前日期的年份
        var month = arr[0]; //获取当前日期的月份
        var year2 = year;
        var month2 = parseInt(month) + 1;
        if (month2 == 13) {
            year2 = parseInt(year2) + 1;
            month2 = 1;
        }
        month2 = '' + month2;
        if (month2.length < 2) month2 = '0' + month2;
        $(defaults.container).html(month2 + "月" + ' ' + year2);

    }

    var getNextDate = function(date, day){
        var dd = new Date(date);
        dd.setDate(dd.getDate() + day);
        var y = dd.getFullYear();
        var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
        var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
        $(defaults.container).html(y + "年 " + m + "月 " + d + "日");
        return {year:y,month:m,day:d};
    }

    // 事件绑定放在for循环里面，没起作用
    var liClickEvent = function (id,data)
    {
        id.click(function (e){
          ChangeMonthOrDay(data);
        })
    }

    var getDateList = function (date)
    {
        $(".content-right").removeClass("dayDetail");
        var arr = date.split('月 ');
        var month = arr[0]; //获取当前日期的月份
        var year = arr[1].replace('年','').trim();
        var month2 = month;
        defaults.myDate.setDate(1);// 这就是1号
        defaults.myDate.setMonth(month2);
        defaults.myDate.setDate(0);// 这两句得到当月最后一天
        var end = defaults.myDate.getDate();// 最后一天的日，比如8月就是31
        $(".date").html('');
        for (var i = 0; i < end; i++) {
            var day = i + 1;
            if(day < 10){
                day = "0"+day;
            }
            $div = $("<li id='day"+ day +"'> "+ day +" </li>");
            $(".date").append($div);
            liClickEvent($div, {year:year, month:month, day:day});
        }
        dateSum = end;
        dateLiWidth(); // 给 date li 设置宽度
    }

    var dateLiWidth = function ()
    {
        var dateWidth = $(".date").width();
        if (dateWidth > 0){
            var liWidth = dateWidth / dateSum;
            $(".date li").width(liWidth - 1);
        }else {
            dateLiWidth();
        }
    }

    var tableLi = function ()
    {
        if(defaults.monthOrDay == "month")
        {
            $(".content-right-con").html('');
            var contentLeftCon = $(".content-left-con li").length;
            var tableLiTotal = contentLeftCon * dateSum;
            for (var j = 0; j < tableLiTotal; j++) {
                $(".content-right-con").append("<li></li>");
            }
            $(".content-right-con li:nth-child(" + dateSum + "n)").css('border-right', '0');
            var dateWidth = $(".date").width();
            var liWidth = dateWidth / dateSum;
            $(".content-right-con li").width(liWidth - 1);
        }
        else
        {
            $(".content-right-con").html('');
            var contentLeftCon = $(".content-left-con li").length;
            for (var j = 0; j < contentLeftCon; j++) {
                $(".content-right-con").append("<li></li>");
            }
        }

    }

    var getStateLine = function (index, startDate, endDate,data)
    {
        //随机数
        var randomString = Math.random().toString(36).substr(2);
        switch (data.orderState) {
            case 1:
                $div = $("<div class='stateChartLine wxf' id='stateChartLine" + index + randomString + "'></div>");
                break;
            case 2:
                $div = $("<div class='stateChartLine yxf' id='stateChartLine" + index + randomString + "'></div>");
                break;
            case 3:
                $div = $("<div class='stateChartLine jxz' id='stateChartLine" + index + randomString + "'></div>");
                break;
            default:
                $div = $("<div class='stateChartLine' id='stateChartLine" + index + randomString + "'></div>");

        }
        $(".stateChart").append($div);
        var contentLeftWidth = $(".content-left").width();//contentLeftWidth 宽度
        var dateLiWidth = $(".date li").width();//dateLiWidth宽度
        var $id = 'stateChartLine" + index + randomString + "';
        $div.click(function (e){
            defaults.ClickNode(data);
        })
        var topPosition = 32 + 44 + index * 32 - 20;//top
        if (startDate > 0 && endDate < dateSum + 1) {
            var startPosition = $("#day" + startDate).position().left + (dateLiWidth / 4 * 3);//leftStart
            var endPosition = $("#day" + endDate).position().left + (dateLiWidth / 4);//leftEnd
            var stateWidth = endPosition - startPosition;//stateWidth
            $("#stateChartLine" + index + randomString).css({
                "top": topPosition,
                "left": startPosition,
                "width": stateWidth
            });
        }

        if (startDate == 0 && endDate < dateSum + 1) {
            var startPosition = contentLeftWidth + 1;//left
            var endPosition = $("#day" + endDate).position().left + (dateLiWidth / 4);//leftEnd
            var stateWidth = endPosition - startPosition;//stateWidth
            $("#stateChartLine" + index + randomString).css({
                "top": topPosition,
                "left": startPosition,
                "width": stateWidth,
                "border-top-left-radius": "0",
                "border-bottom-left-radius": "0"
            });
        }

        if (startDate > 0 && endDate > dateSum) {
            var startPosition = $("#day" + startDate).position().left + (dateLiWidth / 4 * 3);//leftStart
            var endPosition = $("#day" + dateSum).position().left + dateLiWidth + 1;//leftEnd
            var stateWidth = endPosition - startPosition;//stateWidth
            $("#stateChartLine" + index + randomString).css({
                "top": topPosition,
                "left": startPosition,
                "width": stateWidth,
                "border-top-right-radius": "0",
                "border-bottom-right-radius": "0"
            });
        }

        if (startDate == 0 && endDate > dateSum) {
            var startPosition = contentLeftWidth + 1;//left
            var endPosition = $("#day" + dateSum).position().left + dateLiWidth + 1;//leftEnd
            var stateWidth = endPosition - startPosition;//stateWidth
            $("#stateChartLine" + index + randomString).css({
                "top": topPosition,
                "left": startPosition,
                "width": stateWidth,
                "border-top-left-radius": "0",
                "border-bottom-left-radius": "0",
                "border-top-right-radius": "0",
                "border-bottom-right-radius": "0"
            });
        }
    }

    var compareDay;
    var compareSort;
    var compareIndex;
    var getSameDay = function (index, day, sort, orderSn,mon,year,showOrderSn)
    {
        if(day == compareDay && index == compareIndex){
            if(sort >= compareSort){
                compareSort = sort;
            }
        }else {
            compareSort = sort;
        }

        //随机数
        var randomString = Math.random().toString(36).substr(2);
        $div = $("<div class='sameDay' id='sameDaystateChartLine" + index + randomString + "' title='"+ showOrderSn +"'><span class='sameDayDetailBtn'>"+ compareSort +"</span></div>");
        $(".stateChart").append($div);
        //点击进入日
        liClickEvent($div, {year:year, month:mon, day:day});

        var dateLiWidth = $(".date li").width();//dateLiWidth宽度

        var startPosition = $("#day" + day).position().left + (dateLiWidth / 7 * 2);//left
        var topPosition = 32 + 44 + index * 32 - 25;//top
        $("#sameDaystateChartLine" + index + randomString).css({"top": topPosition, "left": startPosition});
        //赋值
        compareDay = day;
        compareSort = sort;
        compareIndex = index;

    }

    //月
    var statePosition = function ()
    {
        var currentYearMonth = $(defaults.container).html().split('月 ');// 当前显示的月年
        var data = defaults.data;
        for (var i = 0; i < data.length; i++) {
            var startDate = data[i].startDate;// 开始年月日
            var endDate = data[i].endDate;// 结束年月日
            var startDate0 = startDate.split('-')[0];// 开始 年
            var endDate0 = endDate.split('-')[0];// 结束 年
            var startDate1 = startDate.split('-')[1];// 开始 月
            var endDate1 = endDate.split('-')[1];// 结束 月
            var startDate2 = startDate.split('-')[2];// 开始 日
            var endDate2 = endDate.split('-')[2];// 结束 日
            if (startDate2 == endDate2) { //如果是同一天
                if (startDate0 == currentYearMonth[1] && startDate1 == currentYearMonth[0]) {
                    getSameDay(data[i].indexId, startDate2, data[i].sort,data[i].orderSn,startDate1,startDate0, data[i].showOrderSn);
                }

            } else {
                //如果不是同一天
                if (endDate0 == startDate0 && startDate0 == currentYearMonth[1] && endDate1 > startDate1 && startDate1 == currentYearMonth[0]) {//同年，跨月，开始月状态条
                    endDate2 = dateSum + 5;
                    getStateLine(data[i].indexId, startDate2, endDate2,data[i]);
                } else if (endDate0 == startDate0 && startDate0 == currentYearMonth[1] && endDate1 > startDate1 && endDate1 == currentYearMonth[0]) {//同年，跨月，结束月状态条
                    startDate2 = 0;
                    getStateLine(data[i].indexId, startDate2, endDate2,data[i]);
                } else if (endDate0 == startDate0 && startDate0 == currentYearMonth[1] && endDate1 - startDate1 > 1 && currentYearMonth[0] > startDate1 && currentYearMonth[0] < endDate1) {//同年，跨多个月，去掉开始结束月状态条
                    startDate2 = 0;
                    endDate2 = dateSum + 5;
                    getStateLine(data[i].indexId, startDate2, endDate2,data[i]);
                } else if (endDate0 > startDate0 && startDate0 == currentYearMonth[1] && startDate1 == currentYearMonth[0]) {//跨年，开始年状态条
                    endDate2 = dateSum + 5;
                    getStateLine(data[i].indexId, startDate2, endDate2,data[i]);
                } else if (endDate0 > startDate0 && endDate0 == currentYearMonth[1] && endDate1 == currentYearMonth[0]) {//跨年，结束年状态条
                    startDate2 = 0;
                    getStateLine(data[i].indexId, startDate2, endDate2,data[i]);
                } else if (endDate0 > startDate0 && endDate1 - startDate1 > -11 && Number(Number(currentYearMonth[0]) + 12) > Number(startDate1) && Number(currentYearMonth[0]) < Number(endDate1) && Number(Number(startDate0) + 1) == Number(currentYearMonth[1]) && Number(endDate0) == Number(currentYearMonth[1])) {//跨年，跨多个月，当前月在年后
                    startDate2 = 0;
                    endDate2 = dateSum + 5;
                    getStateLine(data[i].indexId, startDate2, endDate2,data[i]);
                } else if (endDate0 > startDate0 && endDate1 - startDate1 > -11 && Number(currentYearMonth[0]) > Number(startDate1) && Number(currentYearMonth[0]) < Number(Number(endDate1) + 12) && Number(startDate0) == Number(currentYearMonth[1]) && Number(Number(endDate0) - 1) == Number(currentYearMonth[1])) {//跨年，跨多个月，当前月在年前
                    startDate2 = 0;
                    endDate2 = dateSum + 5;
                    getStateLine(data[i].indexId, startDate2, endDate2,data[i]);
                } else if (endDate0 == startDate0 && startDate0 == currentYearMonth[1] && startDate1 == currentYearMonth[0]) {//同年，同月
                    getStateLine(data[i].indexId, startDate2, endDate2,data[i]);
                }
            }
        }
    }

    //日
    var getStateLine_dayDetail = function(index, orderSameSum, e, data)
    {
        //随机数
        var randomString = Math.random().toString(36).substr(2);
        switch (data.orderState) {
            case 1:
                $div = $("<div class='stateChartLine wxf stateChartLine" + index + "' id='stateChartLine" + index + randomString + "'></div>");
                break;
            case 2:
                $div = $("<div class='stateChartLine yxf stateChartLine" + index + "' id='stateChartLine" + index + randomString + "'></div>");
                break;
            case 3:
                $div = $("<div class='stateChartLine jxz stateChartLine" + index + "' id='stateChartLine" + index + randomString + "'></div>");
                break;
            default:
                $div = $("<div class='stateChartLine stateChartLine" + index + "' id='stateChartLine" + index + randomString + "'></div>");

        }
        $(".stateChart").append($div);
        $div.click(function (e){
            defaults.ClickNode(data);
        })
        var contentLeftWidth = $(".content-left").width();//contentLeftWidth 宽度
        var contentRightWidth = $(".content-right").width();//contentRightWidth 宽度
        var startPosition = contentLeftWidth + 1;//left
        var topPosition = 32 + 44 + index * 32 - 20;//top
        $("#stateChartLine" + index + randomString).css({
            "left": startPosition + (40 * orderSameSum / (orderSameSum + 1)) + ((contentRightWidth / orderSameSum) - 40) * e + (40 * orderSameSum / (orderSameSum + 1)) * e,
        });
        $(".stateChartLine" + index).css({
            "top": topPosition,
            "width": (contentRightWidth / orderSameSum) - 40,
        });
    }

    //日
    var statePosition_dayDetail = function()
    {
        var currentYearMonthDay = $(defaults.container).html().split(' ');// 当前显示的年月日
        var currentYMD_year = currentYearMonthDay[0].replace("年", "");
        var currentYMD_month = currentYearMonthDay[1].replace("月", "");
        var currentYMD_day = currentYearMonthDay[2].replace("日", "");
        var currentDate = currentYMD_year + "-" + currentYMD_month + "-" + currentYMD_day;
        if(defaults.data.length > 0)
        {
            for (var i = 0; i < defaults.data.length; i++) {

                var startDate = defaults.data[i].startDate;// 开始年月日
                var endDate = defaults.data[i].endDate;// 结束年月日
                var startDate0 = startDate.split('-')[0];// 开始 年
                var endDate0 = endDate.split('-')[0];// 结束 年
                var startDate1 = startDate.split('-')[1];// 开始 月
                var endDate1 = endDate.split('-')[1];// 结束 月
                var startDate2 = startDate.split('-')[2];// 开始 日
                var endDate2 = endDate.split('-')[2];// 结束 日

                /*当天工单数量*/
              //  if ((startDate0 == currentYMD_year || endDate0 == currentYMD_year) && (startDate1 == currentYMD_month || endDate1 || (startDate1 < currentYMD_month && currentYMD_month < endDate1)) && (startDate2 == currentYMD_day || endDate2 == currentYMD_day || (startDate2 < currentYMD_day && currentYMD_day < endDate2))) {
                if(currentDate >=startDate && currentDate<= endDate )
                {
                    orderArr.push(defaults.data[i]);
                }
            }
            /*把相同的值放到同一个数组里*/
            var aa = [];
            for (var r = 0; r < orderArr.length; r++) {
                var n = aa.indexOf(orderArr[r].indexId);
                if (n == -1) {
                    aa.push(orderArr[r].indexId);
                    orderSameArr.push([orderArr[r]])
                } else {
                    orderSameArr[n].push(orderArr[r])
                }
            }
            /*遍历出相同工单的状态条*/
            for (var e = 0; e < orderSameArr.length; e++) {
                for (var k = 0; k < orderSameArr[e].length; k++) {
                    getStateLine_dayDetail(orderSameArr[e][k].indexId, orderSameArr[e].length, k,orderSameArr[e][k]);
                }
            }
        }

    }

    //点击返回月
    $(".returnMonth").click(function () {
        $("#date").html('');
        $(".stateChart").html('');
        $(defaults.container).html('');
        defaults.monthOrDay = 'month';
        var myDate = defaults.myDate;
        year = myDate.getFullYear(); //获取当前年
        mon = '' + (myDate.getMonth() + 1); //获取当前月
        day = '' + myDate.getDate(); //获取当前日
        if (mon.length < 2) mon = '0' + mon;
        if (day.length < 2) day = '0' + day;
        $(defaults.container).html(mon + "月" + ' ' + year);
        orderArr = [];
        orderSameArr = [];
        initeLeft();
        //隐藏返回
        $(".returnMonth").attr("style", "display:none");
    })


}